package com.wulis.provider.basic.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wulis.common.annotation.ApiVersion;
import com.wulis.common.model.dto.IdDto;
import com.wulis.common.model.enums.EnableStateEnum;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import com.wulis.provider.basic.model.dto.InsertOrganizationDto;
import com.wulis.provider.basic.model.dto.QueryOrganizationPageDto;
import com.wulis.provider.basic.model.dto.UpdateOrganizationDto;
import com.wulis.provider.basic.model.vo.QueryOrganizationListVo;
import com.wulis.provider.basic.model.vo.QueryOrganizationPageVo;
import com.wulis.provider.basic.model.vo.QueryOrganizationVo;
import com.wulis.provider.basic.service.OrganizationService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.wulis.common.support.BaseController;

import javax.validation.Valid;
import java.util.List;

import static com.wulis.common.model.enums.VersionEnum.WEB_V_1_0;

/**
 * @author WuliBao
 * @date 2020-05-21
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("web/basic/organization")
@Api(tags = "基础信息管理:机构管理")
public class OrganizationController extends BaseController {
    private final OrganizationService organizationService;
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryOrganizationPage")
    @ApiOperation("查询机构信息列表(分页)")
    @PreAuthorize("@api.hasPermission('web:basic:organization:queryOrganizationPage')")
    public ApiWrapper<IPage<QueryOrganizationPageVo>> queryOrganizationPage(@Valid QueryOrganizationPageDto queryOrganizationPageDto) {
        return ApiWrapMapper.success(organizationService.queryOrganizationPage(queryOrganizationPageDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryOrganizationList")
    @ApiOperation("查询机构信息列表(全量)")
    @PreAuthorize("@api.hasPermission('web:basic:organization:queryOrganizationList')")
    public ApiWrapper<List<QueryOrganizationListVo>> queryOrganizationList() {
        return ApiWrapMapper.success(organizationService.queryOrganizationList(EnableStateEnum.IS_ENABLED.value()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @GetMapping("queryOrganization")
    @ApiOperation("查询机构信息")
    @PreAuthorize("@api.hasPermission('web:basic:organization:queryOrganization')")
    public ApiWrapper<QueryOrganizationVo> queryOrganization(@Valid IdDto idDto) {
        return ApiWrapMapper.success(organizationService.queryOrganization(idDto.getId()));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PostMapping("insertOrganization")
    @ApiOperation("新增机构信息")
    @PreAuthorize("@api.hasPermission('web:basic:organization:insertOrganization')")
    public ApiWrapper<Integer> insertOrganization(@Valid @RequestBody InsertOrganizationDto insertOrganizationDto) {
        return ApiWrapMapper.success(organizationService.insertOrganization(insertOrganizationDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @PutMapping("updateOrganization")
    @ApiOperation("修改机构信息")
    @PreAuthorize("@api.hasPermission('web:basic:organization:updateOrganization')")
    public ApiWrapper<Integer> updateOrganization(@Valid @RequestBody UpdateOrganizationDto updateOrganizationDto) {
        return ApiWrapMapper.success(organizationService.updateOrganization(updateOrganizationDto));
    }
    
    @ApiVersion(WEB_V_1_0)
    @DeleteMapping("deleteOrganization")
    @ApiOperation("删除机构信息")
    @PreAuthorize("@api.hasPermission('web:basic:organization:deleteOrganization')")
    public ApiWrapper<Integer> deleteOrganization(@Valid @RequestBody IdDto idDto) {
        return ApiWrapMapper.success(organizationService.deleteOrganization(idDto.getId()));
    }
}
